package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by aswathik on 6/11/14.
 */
public class ListBook implements MenuItem  {

    private Library library;
    private String menuString;

    ListBook(Library library) {
        this.library = library;
        menuString = "List Book";
    }

    public String getMenuString() {
        return menuString;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ListBook listBook = (ListBook) o;

        if (!library.equals(listBook.library)) return false;
        if (!menuString.equals(listBook.menuString)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = library.hashCode();
        result = 31 * result + menuString.hashCode();
        return result;
    }

    public MenuActionResult performMenuAction(UserInterface userInterface) {

        List<LibraryItem> listOfAvailableBooks = new ArrayList<LibraryItem>();
        MenuActionResult menuActionResult = new MenuActionResult();

        listOfAvailableBooks = library.getAvailableBookList();

        userInterface.displayResultString(String.format("%-40s%-40s%-40s", "Title", "Author", "Year Of Publication"));
        userInterface.displayResultString("\n====================================================================================================");

        for ( int i = 0; i < listOfAvailableBooks.size(); i++)
            userInterface.displayLibraryItemDetails((Book)listOfAvailableBooks.get(i));

        if ( listOfAvailableBooks.size() != 0) {
            menuActionResult.setMenuActionDetails(true,"List of Books are displayed");
        } else {
            menuActionResult.setMenuActionDetails(false, "Sorry there are no valid books to display");
        }


        userInterface.displayMenuActionResult(menuActionResult);
        return menuActionResult;
    }
}
